package com.wiz.web.manager.fatory;

import org.springframework.stereotype.Service;

import com.wiz.common.Constant;
import com.wiz.common.TencentSignature;
import com.wiz.vo.criteria.PayCriteria;
import com.wiz.vo.to.tencent.WxBaseData;
import com.wiz.vo.to.tencent.WxPayReqData;
import com.wiz.vo.type.PayMethodType;

/**
 * 获取微信支付数据模板
 * @author Hakeny
 */
@Service("wxDataFactory")
public class WxDataFactory implements Constant{
	
	/**
	 * 根据支付方式不同选择不同data模板
	 * @param criteria
	 * @param type
	 * @return
	 */
	public WxBaseData getDataByType(PayCriteria criteria,String key,int type){
		if(PayMethodType.BAR_PAY.getId() == type){//商户代理商的条码支付
			return getScanPayReqData(criteria,key);
		}else if(PayMethodType.QUERY.getId() == type){
			return getQueryReqData(criteria,key);
		}else if(PayMethodType.REFUND.getId() == type){
			return getRefundReqData(criteria,key);
		}else if(PayMethodType.REVERSE.getId() == type){
			return getReverseReqData(criteria,key);
		}else if(PayMethodType.CHARGE.getId() == type){
			return getUnifiledReqData(criteria,key);
		}else if(PayMethodType.WEBCLOSE.getId() == type){
			return getWebCloseReqData(criteria,key);
		}
		return null;
	}
	
	/**
	 * 获取扫码支付参数
	 * @param criteria
	 * @return
	 */
	private WxPayReqData getScanPayReqData(PayCriteria criteria,String key){
		WxPayReqData data = new WxPayReqData();
//		data.setSub_mch_id("1324420101");
		data.setSub_mch_id(criteria.getSubMchId());
		data.setOut_trade_no(criteria.getOutTradeNo());
		data.setBody(criteria.getBody());
		data.setDevice_info(criteria.getDeviceInfo());
		data.setAttach(criteria.getAttach());
		data.setTotal_fee(criteria.getTotalFee());
		data.setAuth_code(criteria.getAuthCode());
		String sign = TencentSignature.getSign(data.toMap(),key);
		data.setSign(sign);//把签名数据设置到Sign这个属性中
		return data;
	}
	
	/**
	 * 获取扫码查询
	 * @param criteria
	 * @return
	 */
	private WxPayReqData getQueryReqData(PayCriteria criteria,String key){
		WxPayReqData data = new WxPayReqData();
		data.setOut_trade_no(criteria.getOutTradeNo());
		data.setSub_mch_id(criteria.getSubMchId());
//		data.setSub_mch_id("1324420101");
		data.setTransaction_id(criteria.getTransactionId());
		String sign = TencentSignature.getSign(data.toMap(),key);
		data.setSign(sign);//把签名数据设置到Sign这个属性中
		return data;
	}
	
	/**
	 * 获取扫码退款参数
	 * @param criteria
	 * @return
	 */
	private WxPayReqData getRefundReqData(PayCriteria criteria,String key){
		WxPayReqData data = new WxPayReqData();
		data.setOut_trade_no(criteria.getOutTradeNo());
		data.setSub_mch_id(criteria.getSubMchId());
//		data.setSub_mch_id("1324420101");
//		data.setTransaction_id(criteria.getTransactionId());
		data.setTransaction_id(criteria.getTransactionId());
		data.setOut_trade_no(criteria.getOutTradeNo());
		data.setOut_refund_no(criteria.getOutRefundNo());
		data.setTotal_fee(criteria.getTotalFee());
		data.setRefund_fee(criteria.getRefundFee());
		data.setOp_user_id(criteria.getOpUserId());
		String sign = TencentSignature.getSign(data.toMap(),key);
		data.setSign(sign);//把签名数据设置到Sign这个属性中
		return data;
	}
	
	/**
	 * 获取取消扫码支付
	 * @param criteria
	 * @return
	 */
	private WxPayReqData getReverseReqData(PayCriteria criteria,String key){
		WxPayReqData data = new WxPayReqData();
		data.setSpbill_create_ip(null);
		data.setSub_mch_id(criteria.getSubMchId());
		data.setTransaction_id(criteria.getTransactionId());
		data.setOut_trade_no(criteria.getOutTradeNo());
		String sign = TencentSignature.getSign(data.toMap(),key);
		data.setSign(sign);//把签名数据设置到Sign这个属性中
		return data;
	}
	
	/**
	 * web支付下单参数
	 * @param criteria
	 * @param key
	 * @return
	 */
	private WxPayReqData getUnifiledReqData(PayCriteria criteria,String key){
		WxPayReqData data = new WxPayReqData();
		data.setSub_mch_id(criteria.getSubMchId());
		data.setBody(criteria.getBody());
		data.setOut_trade_no(criteria.getOutTradeNo());
		data.setTotal_fee(criteria.getTotalFee());
		data.setNotify_url(WEB_PAY_NOTIFY_URL);
		data.setTrade_type(TRADE_TYPE_JSAPI);
		data.setOpenid(criteria.getOpenId());
		String sign = TencentSignature.getSign(data.toMap(),key);
		data.setSign(sign);//把签名数据设置到Sign这个属性中
		return data;
	}
	
	/**
	 * 获取取消web支付
	 * @param criteria
	 * @return
	 */
	private WxPayReqData getWebCloseReqData(PayCriteria criteria,String key){
		WxPayReqData data = new WxPayReqData();
		data.setSpbill_create_ip(null);
		data.setSub_mch_id(criteria.getSubMchId());
		data.setOut_trade_no(criteria.getOutTradeNo());
		String sign = TencentSignature.getSign(data.toMap(),key);
		data.setSign(sign);//把签名数据设置到Sign这个属性中
		return data;
	}
}
